//
//  newUserViewController.m
//  i18n
//
//  Created by Patrick De Caluwe on 10/2/11.
//  Copyright 2011 patrick.decaluwe@student.uclouvain.be. All rights reserved.
//

#import "newUserViewController.h"

@implementation newUserViewController

@synthesize name;
@synthesize scrollView;
@synthesize pushTimePicker;

-(NSString *) documentsPath 
{
    NSArray *paths = 
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return documentsDir;
}

-(void) writeToFile:(NSString *)text withFileName:(NSString *)filePath
{
    NSMutableArray *array=[[NSMutableArray alloc] init];
    [array addObject:text];
    [array addObject:text];
    [array addObject:text];
    [array writeToFile:filePath atomically:YES];
    [array release];
}

-(NSString *) readFromFile:(NSString *)filePath
{
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        NSString *data= [NSString stringWithFormat:@"%@",[array objectAtIndex:0]];
        [array release];
        return data;
    }
    else{return nil;}
}

-(IBAction)doneEditing:(id)sender
{
    [sender resignFirstResponder];
}

-(IBAction)bgTouched:(id)sender
{
    [name resignFirstResponder];
}

-(IBAction)saveTouched:(id)sender
{
    [[NSUserDefaults standardUserDefaults] setObject:[name text] forKey:@"userid"];
    [[NSUserDefaults standardUserDefaults] setObject:[pushTimePicker date] forKey:@"pushtime"];
 
	[[GlobalData sharedGlobalData] doAlarm];
    [self dismissThisView];
}

-(IBAction)homeButtonClicked:(id)sender
{
    [self dismissThisView];
}

- (void)dismissThisView
{
    [self dismissModalViewControllerAnimated:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // set background color
    scrollView.frame = CGRectMake(0, 0, 320, 460);
    [scrollView setContentSize:CGSizeMake(320, 713)]; 

    // show settings value
    [name setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"userid"]];
    NSDate *pushTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"pushtime"];
    
    if (pushTime != nil)
        [pushTimePicker setDate:pushTime];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
